﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ComponentModel;

namespace CrazyWorship1
{
    [XmlRoot("SongRoot")]
    public class Song : INotifyPropertyChanged
    {
        SyncStatus _syncStatus;
        DateTime _lastUpdate;
        Status _status;
        string _fileName;
        string _name;
        string _songXml;
        string _baseFolderName;
        private Song()
        { }
        public Song(string file)
        {
            _songXml = File.ReadAllText(file);
            _lastUpdate = File.GetLastWriteTime(file);
            _fileName = file;
            _status = Status.Loaded;
        }
        public DateTime LastUpdate
        {
            get
            { return _lastUpdate; }

        }
        public SyncStatus SyncStatus
        {
            get
            { return _syncStatus; }
            set
            {
                _syncStatus = value;
                _syncStatus.PropertyChanged += new PropertyChangedEventHandler(_syncStatus_PropertyChanged);
                _status = Status.NotSynced;
            }
        }

        void _syncStatus_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NotifyPropertyChanged("Status");
        }
        public Status Status
        {
            get
            {
                Status status = _syncStatus.Status;
                foreach (SyncStatus s in SyncStatus.Children)
                {
                    if (status < s.Status)
                        status = s.Status;
                }
                return status;

            }
            set
            {
                _status = value;
                NotifyPropertyChanged("Status");
            }
        }

        public string SongFileName
        {
            get
            { return _fileName; }
            set
            { _fileName = value; }
        }

        public string Name
        {
            get
            { return _name; }
            set
            { _name = value; }
        }
        public string SongXml
        {
            get
            { return _songXml; }
            set
            { _songXml = value; }
        }
        public string BaseFolderName
        {
            get
            { return _baseFolderName; }
            set
            { _baseFolderName = value; }
        }
        public string SongName
        {
            get
            {
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                try
                {
                    return r["song"]["info"]["SongName"].InnerText;
                }
                catch (Exception)
                {
                    return "Untitled";
                }
            }
        }
        public List<string> Resources
        {
            get
            {
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                List<string> temp = new List<string>();
                try
                {
                    foreach (XmlNode n in r["song"]["resources"])

                        temp.Add(n.InnerText);
                }
                catch (NullReferenceException)
                {
                    Console.WriteLine("OK - No Resources");
                }
                return temp;
            }
        }
        public string Author
        {
            get
            {
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                try
                {
                    return r["song"]["info"]["Author"].InnerText;
                }
                catch (Exception)
                {
                    return "No Author";
                }
            }
        }
        public string Copyright
        {
            get
            {
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                try
                {
                    return r["song"]["info"]["Copyright"].InnerText;
                }
                catch (Exception)
                {
                    return "No Copyright";
                }
            }
        }

        public string CCLI
        {
            get
            {
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                try
                {
                    return r["song"]["info"]["CCLI"].InnerText;
                }
                catch (Exception)
                {
                    return "No CCLI";
                }
            }
        }

        public List<string> ScreenAll
        {
            get
            {
                List<string> ret = new List<string>();
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                foreach (XmlNode s in r["song"]["screens"]["all"].ChildNodes)
                    if (s is XmlElement)
                        ret.Add(s.OuterXml);
                return ret;
            }
        }
        public List<string> Controllers
        {
            get
            {
                List<string> ret = new List<string>();
                XmlDocument r = new XmlDocument();
                r.LoadXml(SongXml);
                foreach (XmlNode s in r["song"]["controllers"].ChildNodes)
                    if (s is XmlElement)
                        ret.Add(s.OuterXml);
                return ret;
            }
        }



        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

}
